<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.1.1">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/avatar.jpg">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/avatar.jpg">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/avatar.jpg">
  <link rel="mask-icon" href="/images/avatar.jpg" color="#222">
  <meta name="msvalidate.01" content="4AF2328B2035748153A041B7EEF3D15C">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Noto+Serif+SC:300,300italic,400,400italic,700,700italic%7CLobster:300,300italic,400,400italic,700,700italic%7CPingFang+SC:300,300italic,400,400italic,700,700italic%7CRoboto+Mono:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha256-wiz7ZSCn/btzhjKDQBms9Hx4sSeUYsDrTLg7roPstac=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.33/fancybox/fancybox.css" integrity="sha256-gkQVf8UKZgQ0HyuxL/VnacadJ+D2Kox2TCEBuNQg5+w=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"ethanliu6.github.io","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.19.2","exturl":true,"sidebar":{"position":"left","display":"always","padding":12,"offset":18},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":true,"height":300},"bookmark":{"enable":true,"color":"#222","save":"manual"},"mediumzoom":true,"lazyload":true,"pangu":false,"comments":{"style":"buttons","active":"changyan","storage":true,"lazyload":false,"nav":null,"activeClass":"changyan"},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":6,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="JavaSE基础——Day06 该篇主要讲解Java中的常用API:Scanner、Random、GUI">
<meta property="og:type" content="article">
<meta property="og:title" content="JavaSE基础——Day06 JavaSE基础——常用API:Scanner、Random、GUI">
<meta property="og:url" content="http://ethanliu6.github.io/2024/03/19/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day06%E5%B8%B8%E7%94%A8API(1)/index.html">
<meta property="og:site_name" content="&lt;font color&#x3D;&#39;white&#39; &gt;Ethan の Blog&lt;&#x2F;font&gt;">
<meta property="og:description" content="JavaSE基础——Day06 该篇主要讲解Java中的常用API:Scanner、Random、GUI">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/19/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day06%E5%B8%B8%E7%94%A8API(1)/imgs/1640156776779.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/19/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day06%E5%B8%B8%E7%94%A8API(1)/imgs/1640156898821.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/19/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day06%E5%B8%B8%E7%94%A8API(1)/imgs/1640157051547.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/19/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day06%E5%B8%B8%E7%94%A8API(1)/imgs/1640157126304.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/19/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day06%E5%B8%B8%E7%94%A8API(1)/imgs/1640157196012.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/19/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day06%E5%B8%B8%E7%94%A8API(1)/imgs/1640157257413.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/19/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day06%E5%B8%B8%E7%94%A8API(1)/imgs/1640157357609.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/19/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day06%E5%B8%B8%E7%94%A8API(1)/imgs/1640157422163.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/19/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day06%E5%B8%B8%E7%94%A8API(1)/imgs/1640157499890.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/19/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day06%E5%B8%B8%E7%94%A8API(1)/imgs/1640157613280.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/19/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day06%E5%B8%B8%E7%94%A8API(1)/imgs/1640157673262.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/19/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day06%E5%B8%B8%E7%94%A8API(1)/imgs/1640158945337.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/19/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day06%E5%B8%B8%E7%94%A8API(1)/imgs/1640158964252.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/19/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day06%E5%B8%B8%E7%94%A8API(1)/imgs/1640159115023.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/19/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day06%E5%B8%B8%E7%94%A8API(1)/imgs/1640160198618.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/19/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day06%E5%B8%B8%E7%94%A8API(1)/imgs/1640160345308.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/19/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day06%E5%B8%B8%E7%94%A8API(1)/imgs/1640160399176.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/19/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day06%E5%B8%B8%E7%94%A8API(1)/imgs/1640160482727.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/19/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day06%E5%B8%B8%E7%94%A8API(1)/imgs/1640160461955.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/19/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day06%E5%B8%B8%E7%94%A8API(1)/imgs/1640160575426.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/19/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day06%E5%B8%B8%E7%94%A8API(1)/imgs/1640160765732.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/19/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day06%E5%B8%B8%E7%94%A8API(1)/imgs/1640161060785.png">
<meta property="article:published_time" content="2024-03-18T16:00:00.000Z">
<meta property="article:modified_time" content="2024-04-26T05:13:50.545Z">
<meta property="article:author" content="Ethan Liu">
<meta property="article:tag" content="Java">
<meta property="article:tag" content="Java基础">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://ethanliu6.github.io/2024/03/19/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day06%E5%B8%B8%E7%94%A8API(1)/imgs/1640156776779.png">


<link rel="canonical" href="http://ethanliu6.github.io/2024/03/19/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day06%E5%B8%B8%E7%94%A8API(1)/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://ethanliu6.github.io/2024/03/19/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day06%E5%B8%B8%E7%94%A8API(1)/","path":"2024/03/19/JavaNotes/JavaSE基础/day06常用API(1)/","title":"JavaSE基础——Day06 JavaSE基础——常用API:Scanner、Random、GUI"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>JavaSE基础——Day06 JavaSE基础——常用API:Scanner、Random、GUI | <font color='white' >Ethan の Blog</font></title>
  








  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<style>.darkmode--activated{--body-bg-color:#282828;--content-bg-color:#333;--card-bg-color:#555;--text-color:#ccc;--blockquote-color:#bbb;--link-color:#ccc;--link-hover-color:#eee;--brand-color:#ddd;--brand-hover-color:#ddd;--table-row-odd-bg-color:#282828;--table-row-hover-bg-color:#363636;--menu-item-bg-color:#555;--btn-default-bg:#222;--btn-default-color:#ccc;--btn-default-border-color:#555;--btn-default-hover-bg:#666;--btn-default-hover-color:#ccc;--btn-default-hover-border-color:#666;--highlight-background:#282b2e;--highlight-foreground:#a9b7c6;--highlight-gutter-background:#34393d;--highlight-gutter-foreground:#9ca9b6}.darkmode--activated img{opacity:.75}.darkmode--activated img:hover{opacity:.9}.darkmode--activated code{color:#69dbdc;background:0 0}button.darkmode-toggle{z-index:9999}.darkmode-ignore,img{display:flex!important}.beian img{display:inline-block!important}</style></head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title"><font color='white' >Ethan の Blog</font></p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description"><font color='#FDABC1' size=3>清醒的停留，胜过——盲目的前行</font></p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-关于我"><a href="/aboutme/" rel="section"><i class="fa fa-user fa-fw"></i>关于我</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签<span class="badge">18</span></a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类<span class="badge">14</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档<span class="badge">53</span></a></li><li class="menu-item menu-item-love"><a href="/categories/Love/" rel="section"><i class="fas fa-heart fa-fw"></i>Love</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#api-%E6%A6%82%E8%BF%B0"><span class="nav-text">1：API 概述</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#api%E6%A6%82%E8%BF%B0%E5%92%8C%E8%AF%BE%E7%A8%8B%E5%AE%89%E6%8E%92"><span class="nav-text">1.1 API概述和课程安排</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8C%85%E5%92%8C%E5%AF%BC%E5%8C%85"><span class="nav-text">1.2 包和导包</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#scanner"><span class="nav-text">2：Scanner</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#scanner%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8"><span class="nav-text">2.1 Scanner基本使用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%BB%83%E4%B9%A0%E6%95%B0%E6%8D%AE%E6%B1%82%E5%92%8C"><span class="nav-text">2.2 练习(数据求和)</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#random"><span class="nav-text">3：Random</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#random%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8"><span class="nav-text">3.1 Random基本使用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%BB%83%E4%B9%A0%E7%8C%9C%E6%95%B0%E5%AD%97%E6%B8%B8%E6%88%8F"><span class="nav-text">3.2 练习(猜数字游戏)</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#gui"><span class="nav-text">4：GUI</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#gui%E6%A6%82%E8%BF%B0"><span class="nav-text">4.1 GUI概述</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B8%B8%E7%94%A8%E7%BB%84%E4%BB%B6"><span class="nav-text">4.2 常用组件</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#jframe%E5%88%9D%E8%AF%86%E7%AA%97%E4%BD%93"><span class="nav-text">4.2.1 JFrame(初识窗体)</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#jframe%E5%B8%B8%E7%94%A8%E8%AE%BE%E7%BD%AE"><span class="nav-text">4.2.2 JFrame(常用设置)</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#jbutton%E7%AA%97%E5%8F%A3%E4%B8%AD%E6%B7%BB%E5%8A%A0%E6%8C%89%E9%92%AE"><span class="nav-text">4.2.3 JButton(窗口中添加按钮)</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#jlabel%E6%98%BE%E7%A4%BA%E6%96%87%E6%9C%AC%E5%92%8C%E5%9B%BE%E5%83%8F"><span class="nav-text">4.2.4 JLabel(显示文本和图像)</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%A1%88%E4%BE%8B"><span class="nav-text">4.3 案例</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%A1%88%E4%BE%8B1%E7%94%A8%E6%88%B7%E7%99%BB%E5%BD%95"><span class="nav-text">4.3.1 案例1(用户登录)</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%A1%88%E4%BE%8B2%E8%81%8A%E5%A4%A9%E5%AE%A4"><span class="nav-text">4.3.2 案例2(聊天室)</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%A1%88%E4%BE%8B3%E7%8C%9C%E6%95%B0%E5%AD%97"><span class="nav-text">4.3.3 案例3(猜数字)</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%A1%88%E4%BE%8B4%E6%89%8B%E6%9C%BA%E6%97%A5%E6%9C%9F%E5%92%8C%E6%97%B6%E9%97%B4%E6%98%BE%E7%A4%BA"><span class="nav-text">4.3.4 案例4(手机日期和时间显示)</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%A1%88%E4%BE%8B5%E8%80%83%E5%8B%A4%E6%9F%A5%E8%AF%A2"><span class="nav-text">4.3.5 案例5(考勤查询)</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%A1%88%E4%BE%8B5%E8%80%83%E5%8B%A4%E6%9F%A5%E8%AF%A2%E4%B9%8B%E6%97%A5%E5%8E%86%E6%8E%A7%E4%BB%B6"><span class="nav-text">4.3.6 案例5(考勤查询之日历控件)</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BA%8B%E4%BB%B6%E7%9B%91%E5%90%AC%E6%9C%BA%E5%88%B6"><span class="nav-text">4.4 事件监听机制</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Ethan Liu"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">Ethan Liu</p>
  <div class="site-description" itemprop="description"><font color='#0000FF' size=1.5px>语言学 | NLP | Py | Java</font></div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">53</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">14</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">18</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL2V0aGFubGl1Ng==" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;ethanliu6"><i class="fab fa-github fa-fw"></i>GitHub</span>
      </span>
      <span class="links-of-author-item">
        <span class="exturl" data-url="bWFpbHRvOmxpdWUyMzM2QGdtYWlsLmNvbQ==" title="E-Mail → mailto:liue2336@gmail.com"><i class="fa fa-envelope fa-fw"></i>E-Mail</span>
      </span>
      <span class="links-of-author-item">
        <span class="exturl" data-url="aHR0cHM6Ly9zcGFjZS5iaWxpYmlsaS5jb20vMTMyNzA5OTk3Nw==" title="BiliBili → https:&#x2F;&#x2F;space.bilibili.com&#x2F;1327099977"><i class="fa-brands fa-bilibili fa-fw"></i>BiliBili</span>
      </span>
      <span class="links-of-author-item">
        <a href="/categories/Love/" title="Love → &#x2F;categories&#x2F;Love&#x2F;" rel="noopener me"><i class="fa fa-heart fa-fw"></i>Love</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://ethanliu6.github.io/2024/03/19/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day06%E5%B8%B8%E7%94%A8API(1)/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="Ethan Liu">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="<font color='white' >Ethan の Blog</font>">
      <meta itemprop="description" content="<font color='#0000FF' size=1.5px>语言学 | NLP | Py | Java</font>">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="JavaSE基础——Day06 JavaSE基础——常用API:Scanner、Random、GUI | <font color='white' >Ethan の Blog</font>">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          JavaSE基础——Day06 JavaSE基础——常用API:Scanner、Random、GUI<span class="exturl post-edit-link" data-url="aHR0cHM6Ly9naXRodWIuY29tL0V0aGFuTGl1Ni9ldGhhbmxpdTYuZ2l0aHViLmlvL3RyZWUvbWFzdGVyL19wb3N0cy9KYXZhTm90ZXMvSmF2YVNF5Z+656GAL2RheTA25bi455SoQVBJKDEpLm1k" title="编辑"><i class="fa fa-pen-nib"></i></span>
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2024-03-19 00:00:00" itemprop="dateCreated datePublished" datetime="2024-03-19T00:00:00+08:00">2024-03-19</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2024-04-26 13:13:50" itemprop="dateModified" datetime="2024-04-26T13:13:50+08:00">2024-04-26</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Java%E5%85%A8%E6%A0%88/" itemprop="url" rel="index"><span itemprop="name">Java全栈</span></a>
        </span>
    </span>

  
    <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">阅读次数：</span>
      <span id="busuanzi_value_page_pv"></span>
    </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Changyan：</span>
    
    <a title="JavaSE基础——Day06 JavaSE基础——常用API:Scanner、Random、GUI" href="/2024/03/19/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day06%E5%B8%B8%E7%94%A8API(1)/#SOHUCS" itemprop="discussionUrl">
      <span id="sourceId::310206cc29688fe458e006a7c7db7957" class="cy_cmt_count" itemprop="commentCount"></span>
    </a>
  </span>
  
  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>5.8k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>21 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><blockquote class="blockquote-center">
<p>JavaSE基础——Day06
该篇主要讲解Java中的<strong>常用API:Scanner、Random、GUI</strong></p>

</blockquote>
<span id="more"></span>
<p><strong>今日目标：</strong></p>
<ul>
<li>能够知道什么是API</li>
<li>能够使用帮助文档学习API</li>
<li>能够理解包和导包</li>
<li>能够知道Scanner的作用和基本使用</li>
<li>能够知道Random的作用和基本使用</li>
<li>能够知道GUI的作用和基本使用</li>
<li>能够完成GUI相关的授课案例</li>
<li>能够理解事件监听机制</li>
<li>能够对按钮添加事件</li>
</ul>
<h2 id="api-概述">1：API 概述</h2>
<h3 id="api概述和课程安排">1.1 API概述和课程安排</h3>
<p>来，继续啊，我们在讲解面向对象的时候，提到了学习面向对象重点就是学习两块内容：</p>
<figure>
<img data-src="imgs/1640156776779.png" alt="1640156776779">
<figcaption aria-hidden="true">1640156776779</figcaption>
</figure>
<p>第一块：学习如何自己设计对象并使用</p>
<p>第二块：学习获取已有对象并使用</p>
<p>而关于第一块知识：学习如何自己设计对象并使用，我们现在已经有一些基础了。</p>
<p>通过前面的学习呢，我们知道了如何定义类，并通过构造方法创建对象，然后通过对象使用成员变量和成员方法。</p>
<p>接下来，我们要学习的是第二块知识：获取已有对象并使用。而获取已有对象并使用呢，在Java中我们习惯称之为常用API的学习。</p>
<p>所以下面我们来学习常用API。首先，我们来说一下什么是API：</p>
<ul>
<li><strong>API(Application Programming Interface)</strong>
：应用程序编程接口</li>
</ul>
<p>那如何理解应用程序编程接口呢？这里我们通过下面的场景给大家解释一下。</p>
<p>看这里，我想编写一个机器人踢足球的程序，那么程序就要向机器人发出向前跑、向后跑、射门、抢球等各种命令。</p>
<figure>
<img data-src="imgs/1640156898821.png" alt="1640156898821">
<figcaption aria-hidden="true">1640156898821</figcaption>
</figure>
<p>如果我自己来编写呢，就会比较麻烦，因为机器人不是我设计的。所以，为了方便其他的开发者使用机器人，</p>
<p>机器人厂商呢就会提供一些用于控制机器人的类，这些类中会定义好操作机器人各种动作的方法。</p>
<p>有了这些类之后，开发一个机器人踢足球的程序就会非常的方便了。</p>
<p>其实，这些类就是机器人厂商提供给开发者的应用程序编程接口，而这些类我们称之为：API</p>
<p>知道了什么是API之后呢，我们来说一下，Java的API。</p>
<ul>
<li><strong>Java API</strong> ：指的就是 JDK 中提供的各种功能的
Java类。</li>
</ul>
<p>这些类将底层的实现封装了起来，我们不需要关心这些类是如何实现的，只需要学习这些类如何使用即可，</p>
<p>为了帮助我们使用这些类，Java还提供了对应的帮助文档。我们可以通过帮助文档来学习这些API如何使用</p>
<p>下面呢，我们一起来学习一下<strong>帮助文档的使用</strong>：</p>
<p>要想使用帮助文档，首先我们得有一个帮助文档：来，打开课程资料下面，我们这里有一个帮助文档。</p>
<figure>
<img data-src="imgs/1640157051547.png" alt="1640157051547">
<figcaption aria-hidden="true">1640157051547</figcaption>
</figure>
<p>这个文档对应的是JDK11版本的，是目前比较新的中文版的帮助文档。</p>
<p>有了文档之后呢，我们来打开帮助文档。</p>
<p>文档打开后，找到索引这里，下面有一个输入框，在这个框框里输入我们要学习的API。</p>
<figure>
<img data-src="imgs/1640157126304.png" alt="1640157126304">
<figcaption aria-hidden="true">1640157126304</figcaption>
</figure>
<p>这里，我们拿字符串类做一个说明：在这个框框里面我们输入：String，然后一回车，看到一个新的界面</p>
<p>这个界面中，就是关于String相关的知识了。</p>
<figure>
<img data-src="imgs/1640157196012.png" alt="1640157196012">
<figcaption aria-hidden="true">1640157196012</figcaption>
</figure>
<p>接着，我们来说一下，在学习一个API的时候，<strong>我们重点要看四个地方：</strong></p>
<p><strong>第一个地方：看类所在的包</strong></p>
<figure>
<img data-src="imgs/1640157257413.png" alt="1640157257413">
<figcaption aria-hidden="true">1640157257413</figcaption>
</figure>
<p>为什么要看这里呢，因为java.lang包下的类，我们是可以直接使用的，不需要导包。</p>
<p>其他包下的类，我们都是要导包后才能使用的，至于如何导包，下个知识我们会讲解。</p>
<p>很明显，String类在使用的时候，我们是不需要导包的。</p>
<p><strong>第二个地方：看类的描述</strong></p>
<p>也就是看这里，这里一般描述了类是干什么的。</p>
<figure>
<img data-src="imgs/1640157357609.png" alt="1640157357609">
<figcaption aria-hidden="true">1640157357609</figcaption>
</figure>
<p>比如说：String类表示字符串。 Java程序中的所有字符串文字（例如"abc"
）都实现为此类的实例。</p>
<p>告诉了我们String就是用来表示字符串的类，直接写一个字符串数据：abc就是String类的一个对象。</p>
<p><strong>第三个地方：看类的构造方法</strong></p>
<figure>
<img data-src="imgs/1640157422163.png" alt="1640157422163">
<figcaption aria-hidden="true">1640157422163</figcaption>
</figure>
<p>往下滑，这就是String类的构造方法，为什么要看构造方法呢？因为我们在使用一个类的时候，其实使用的是该类的对象，</p>
<p>而创建对象，我们是需要通过构造方法实现的，所以，我们得看看它提供了哪些构造方法。</p>
<p>这里的构造方法提供的比较多，至于用哪个，后面我们用到的时候再讲解，目前知道看哪里就行了</p>
<p><strong>第四个地方：看类的成员方法</strong></p>
<figure>
<img data-src="imgs/1640157499890.png" alt="1640157499890">
<figcaption aria-hidden="true">1640157499890</figcaption>
</figure>
<p>这是我们学习一个API的重点，我们之所以学习一个API，重点就是为了使用该API来完成某些功能。</p>
<p>而API的功能，都是通过一个个方法实现的，所以我们重点得看看类的成员方法。往下滑，这就是String提供的成员方法，有很多。</p>
<p>而一个方法，我们重点又看什么呢？回到第一个方法，我们说一下：</p>
<figure>
<img data-src="imgs/1640157613280.png" alt="1640157613280">
<figcaption aria-hidden="true">1640157613280</figcaption>
</figure>
<p>先看描述，通过描述，我们就知道了方法是干什么的。</p>
<p>再看方法名和参数，这样我们就知道调用哪个方法，以及参数的类型和个数。</p>
<p>最后，我们看返回值类型，这样我们就知道调用该方法后，最终给我们返回了一个什么类型结果</p>
<p>通过这些步骤，我们就知道了所学习的API该如何使用了，然后就可以到IDEA中去使用API了。</p>
<p>知道了帮助文档的使用步骤后，回到资料我们总结一下<strong>帮助文档的使用流程：</strong></p>
<p>① 打开帮助文档</p>
<p>② 找到索引选项，输入要学习的API，然后回车</p>
<p>③ 看类所在包：因为java.lang包下的类在使用的时候不需要导包</p>
<p>④ 看类的描述：这样我们就知道了类是干什么的</p>
<p>⑤ 看类的构造方法：这样我们就可以创建对象了</p>
<p>⑥ 看类的成员方法：这样我们就可以通过这些方法完成某些功能了</p>
<p>知道了什么是API，以及通过帮助文档如何学习API后，我们来说一下常用API这部分内容的课程安排：</p>
<figure>
<img data-src="imgs/1640157673262.png" alt="1640157673262">
<figcaption aria-hidden="true">1640157673262</figcaption>
</figure>
<p>首先，我们学习一下包和导包的基本概念和操作，为API的使用做一个基础铺垫</p>
<p>接着我们来学习两个API：Scanner和Random</p>
<p>Scanner：是用来实现键盘录入数据的</p>
<p>Random：是用来产生随机数的</p>
<p>通过这两个API的学习，可以让我们熟悉帮助文档的使用</p>
<p>再接着我们来学习GUI相关的API，这里不是一个API的学习，而是多个API的学习，通过这部分内容的学习，可以让我们的操作可视化，也就是有界面了，不在仅仅是在控制台输出数据了。</p>
<p>最后，我们学习这几个API，学习这几个API的目的，是为了完成我们在GUI部分未完成的案例。</p>
<p>好了，到此关于API概述和常用API的课程安排我们就先讲到这里</p>
<h3 id="包和导包">1.2 包和导包</h3>
<p>来，继续啊，下面我们来学习一下，包和导包。</p>
<p>那什么是包呢？</p>
<ul>
<li>包其实就是文件夹</li>
<li>作用：对类进行分类管理</li>
</ul>
<p><strong>包的定义格式：</strong></p>
<ul>
<li>格式：package 包名;<br>
</li>
<li>注意：包名一般是公司域名反写，并且多级包用.分开</li>
<li>举例：www.itheima.com</li>
<li>范例：package com.itheima;</li>
</ul>
<p>知道了如何定义包之后，下面我们来定义两个包，并且在不同包下定义同名的类。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima_01;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Student</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">study</span><span class="params">()</span> &#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;好好学习天天向上&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">package</span> com.itheima_01;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">StudentTest</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">Student</span> <span class="variable">s</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Student</span>();</span><br><span class="line">        s.study();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima_02;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">StudentTest</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        com.itheima_01.<span class="type">Student</span> <span class="variable">s</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">com</span>.itheima_01.Student();</span><br><span class="line">        s.study();</span><br><span class="line"></span><br><span class="line">        com.itheima_01.<span class="type">Student</span> <span class="variable">s2</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">com</span>.itheima_01.Student();</span><br><span class="line">        s2.study();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>在这里我们讲解了同一个包下类的使用，以及不同包下类的使用。发现不同包下类的使用太麻烦了，所以，这个时候，我们就要学习一下导包了。</p>
<p>使用不同包下的类时，使用的时候要写类的全路径，写起来太麻烦了。</p>
<p>为了简化带包的操作，Java就提供了导包的功能。</p>
<p><strong>导包的格式：</strong></p>
<ul>
<li>格式：import 包名;</li>
<li>范例：import com.itheima.Student;</li>
</ul>
<p>知道了如何导包后，我们再来修改一下不同包下类的使用代码：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima_02;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.itheima_01.Student;</span><br><span class="line"><span class="comment">//Alt+Enter 快捷键导包</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">StudentTest</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">Student</span> <span class="variable">s</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Student</span>();</span><br><span class="line">        s.study();</span><br><span class="line"></span><br><span class="line">        <span class="type">Student</span> <span class="variable">s2</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Student</span>();</span><br><span class="line">        s2.study();</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//        com.itheima_01.Student s = new com.itheima_01.Student();</span></span><br><span class="line"><span class="comment">//        s.study();</span></span><br><span class="line"><span class="comment">//</span></span><br><span class="line"><span class="comment">//        com.itheima_01.Student s2 = new com.itheima_01.Student();</span></span><br><span class="line"><span class="comment">//        s2.study();</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>在这里我们讲解了导包的三种方式：</p>
<ol type="1">
<li>手动导包</li>
<li>快捷键导包</li>
<li>写一个类，写一部分的时候，如果给出了对应的提示，回车后会自动导包</li>
</ol>
<p>讲解完毕后，大家赶快动手练习一下吧。</p>
<h2 id="scanner">2：Scanner</h2>
<h3 id="scanner基本使用">2.1 Scanner基本使用</h3>
<p>了解了API相关的知识后，下面我们来学习一下Scanner的使用。</p>
<p>通过查看帮助文档，我们知道了如下的信息：</p>
<p><strong>Scanner：</strong></p>
<ul>
<li>一个简单的文本扫描程序，可以获取基本类型数据和字符串数据</li>
</ul>
<p><strong>构造方法：</strong></p>
<ul>
<li>Scanner(InputStream source)：创建 Scanner 对象</li>
<li>System.in：对应的是InputStream类型，可以表示键盘输入</li>
<li>Scanner sc = new Scanner(System.in);</li>
</ul>
<p><strong>成员方法：</strong></p>
<ul>
<li>int nextInt()：获取一个int类型的数据</li>
<li>int i = sc.nextInt();</li>
</ul>
<p>知道了Scanner的构造方法和成员方法后，我们到IDEA中去使用一下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.Scanner;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    Scanner的基本使用</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">ScannerDemo</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//Scanner(InputStream source)：创建 Scanner 对象</span></span><br><span class="line">        <span class="type">Scanner</span> <span class="variable">sc</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Scanner</span>(System.in);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//int nextInt()：获取一个int类型的数据</span></span><br><span class="line">        System.out.println(<span class="string">&quot;请输入一个整数：&quot;</span>);</span><br><span class="line">        <span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> sc.nextInt();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//输出获取到的数据</span></span><br><span class="line">        System.out.println(<span class="string">&quot;你输入的数据是：&quot;</span> + i);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>讲解完毕后，大家赶快动手练习一下吧。</p>
<h3 id="练习数据求和">2.2 练习(数据求和)</h3>
<p>需求：键盘录入两个整数，求两个数据和，并在控制台输出求和结果</p>
<p>首先，我们来简单的分析一下：</p>
<p><strong>分析：</strong></p>
<p>① 创建键盘录入数据对象</p>
<p>② 通过键盘录入数据对象调用nextInt()方法获取数据</p>
<p>③ 对获取的两个整数求和</p>
<p>④ 在控制台输出求和结果</p>
<p>分析完毕后，我们到IDEA中去实现一下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    需求：键盘录入两个整数，求两个数据和，并在控制台输出求和结果</span></span><br><span class="line"><span class="comment">    分析：</span></span><br><span class="line"><span class="comment">        1:创建键盘录入数据对象</span></span><br><span class="line"><span class="comment">        2:通过键盘录入数据对象调用nextInt()方法获取数据</span></span><br><span class="line"><span class="comment">        3:对获取的两个整数求和</span></span><br><span class="line"><span class="comment">        4:在控制台输出求和结果</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">ScannerTest</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//创建键盘录入数据对象</span></span><br><span class="line">        <span class="type">Scanner</span> <span class="variable">sc</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Scanner</span>(System.in);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//通过键盘录入数据对象调用nextInt()方法获取数据</span></span><br><span class="line">        System.out.println(<span class="string">&quot;请输入第一个整数：&quot;</span>);</span><br><span class="line">        <span class="type">int</span> <span class="variable">firstNumber</span> <span class="operator">=</span> sc.nextInt();</span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="string">&quot;请输入第二个整数：&quot;</span>);</span><br><span class="line">        <span class="type">int</span> <span class="variable">secondNumber</span> <span class="operator">=</span> sc.nextInt();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//对获取的两个整数求和</span></span><br><span class="line">        <span class="type">int</span> <span class="variable">sum</span> <span class="operator">=</span> firstNumber + secondNumber;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//在控制台输出求和结果</span></span><br><span class="line">        System.out.println(<span class="string">&quot;求和结果是：&quot;</span> + sum);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>讲解完毕后，大家赶快动手练习一下吧。</p>
<h2 id="random">3：Random</h2>
<h3 id="random基本使用">3.1 Random基本使用</h3>
<p>来，继续啊，下面我们来学习一下Random的使用。</p>
<p>通过查看帮助文档，我们知道了如下的信息：</p>
<p><strong>Random：</strong></p>
<ul>
<li>该类的实例用于生成随机数</li>
</ul>
<p><strong>构造方法：</strong></p>
<ul>
<li>Random()：创建一个新的随机数生成器</li>
</ul>
<p><strong>成员方法：</strong></p>
<ul>
<li>int nextInt(int
bound)：获取一个int类型的随机数，参数bound表示获取到的随机数在[0,bound)之间</li>
</ul>
<p>知道了Random的构造方法和成员方法后，我们到IDEA中去使用一下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.Random;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    Random的基本使用</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">RandomDemo</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//Random()：创建一个新的随机数生成器</span></span><br><span class="line">        <span class="type">Random</span> <span class="variable">r</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Random</span>();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//int nextInt(int bound)：获取一个int类型的随机数，参数bound表示获取到的随机数在[0,bound)之间</span></span><br><span class="line"><span class="comment">//        int i = r.nextInt(10);</span></span><br><span class="line"><span class="comment">//        System.out.println(i);</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//        for (int i=1; i&lt;=10; i++) &#123;</span></span><br><span class="line"><span class="comment">//            int j = r.nextInt(10);</span></span><br><span class="line"><span class="comment">//            System.out.println(j);</span></span><br><span class="line"><span class="comment">//        &#125;</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//需求：如何获取1-100之间的随机数呢？</span></span><br><span class="line"><span class="comment">//        int number = r.nextInt(100);//0-99</span></span><br><span class="line"><span class="comment">//        int number = r.nextInt(101);//0-100</span></span><br><span class="line">        <span class="type">int</span> <span class="variable">number</span> <span class="operator">=</span> r.nextInt(<span class="number">100</span>) + <span class="number">1</span>;</span><br><span class="line">        System.out.println(number);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>讲解完毕后，大家赶快动手练习一下吧。</p>
<h3 id="练习猜数字游戏">3.2 练习(猜数字游戏)</h3>
<p>需求：系统自动产生一个1-100之间的整数，使用程序实现猜这个数字是多少？</p>
<pre><code>猜的时候根据不同情况给出相应的提示

如果猜的数字比真实数字大，提示你猜的数据大了

如果猜的数字比真实数字小，提示你猜的数据小了

如果猜的数字与真实数字相等，提示恭喜你猜中了</code></pre>
<p>首先，我们来简单的分析一下：</p>
<p><strong>分析：</strong></p>
<p>① 使用Random获取一个1-100之间的整数</p>
<p>② 使用Scanner实现键盘录入猜的数据值</p>
<p>③ 使用if…else if…else 的格式实现猜数字，并给出对应的提示</p>
<p>分析完毕后，我们到IDEA中去实现一下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.Random;</span><br><span class="line"><span class="keyword">import</span> java.util.Scanner;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    需求：系统自动产生一个1-100之间的整数，使用程序实现猜这个数字是多少？</span></span><br><span class="line"><span class="comment">        猜的时候根据不同情况给出相应的提示</span></span><br><span class="line"><span class="comment">        如果猜的数字比真实数字大，提示你猜的数据大了</span></span><br><span class="line"><span class="comment">        如果猜的数字比真实数字小，提示你猜的数据小了</span></span><br><span class="line"><span class="comment">        如果猜的数字与真实数字相等，提示恭喜你猜中了</span></span><br><span class="line"><span class="comment">    分析：</span></span><br><span class="line"><span class="comment">        1:使用Random获取一个1-100之间的整数</span></span><br><span class="line"><span class="comment">        2:使用Scanner实现键盘录入猜的数据值</span></span><br><span class="line"><span class="comment">        3:使用if…else if…else 的格式实现猜数字，并给出对应的提示</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">RandomTest</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//使用Random获取一个1-100之间的整数</span></span><br><span class="line">        <span class="type">Random</span> <span class="variable">r</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Random</span>();</span><br><span class="line">        <span class="type">int</span> <span class="variable">number</span> <span class="operator">=</span> r.nextInt(<span class="number">100</span>) + <span class="number">1</span>;</span><br><span class="line">        System.out.println(<span class="string">&quot;系统已经产生了一个1-100之间的整数&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">while</span> (<span class="literal">true</span>) &#123;</span><br><span class="line">            <span class="comment">//使用Scanner实现键盘录入猜的数据值</span></span><br><span class="line">            <span class="type">Scanner</span> <span class="variable">sc</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Scanner</span>(System.in);</span><br><span class="line">            System.out.println(<span class="string">&quot;请输入你要猜的数字：&quot;</span>);</span><br><span class="line">            <span class="type">int</span> <span class="variable">guessNumber</span> <span class="operator">=</span> sc.nextInt();</span><br><span class="line"></span><br><span class="line">            <span class="comment">//使用if…else if…else 的格式实现猜数字，并给出对应的提示</span></span><br><span class="line">            <span class="keyword">if</span> (guessNumber &gt; number) &#123;</span><br><span class="line">                System.out.println(<span class="string">&quot;你猜的数据&quot;</span> + guessNumber + <span class="string">&quot;大了&quot;</span>);</span><br><span class="line">            &#125; <span class="keyword">else</span> <span class="keyword">if</span> (guessNumber &lt; number) &#123;</span><br><span class="line">                System.out.println(<span class="string">&quot;你猜的数据&quot;</span> + guessNumber + <span class="string">&quot;小了&quot;</span>);</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                System.out.println(<span class="string">&quot;恭喜你猜中了&quot;</span>);</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>讲解完毕后，大家赶快动手练习一下吧。</p>
<h2 id="gui">4：GUI</h2>
<h3 id="gui概述">4.1 GUI概述</h3>
<p>来，继续啊，下面呢我们来学习GUI相关的API。</p>
<p>首先呢，我们来说一下，什么是GUI：</p>
<ul>
<li><strong>GUI：</strong>Graphical User Interface(图形用户接口)</li>
<li>用图形的方式，来显示计算机操作的界面</li>
</ul>
<p>怎么理解呢？我们把后面要做的案例拿来举例说明一下：</p>
<p>看这里：</p>
<figure>
<img data-src="imgs/1640158945337.png" alt="1640158945337">
<figcaption aria-hidden="true">1640158945337</figcaption>
</figure>
<p>这是用户登录的案例，在这个界面上，有显示文本的标签，显示输入内容的文本框以及按钮。</p>
<p>再来看一个：</p>
<figure>
<img data-src="imgs/1640158964252.png" alt="1640158964252">
<figcaption aria-hidden="true">1640158964252</figcaption>
</figure>
<p>聊天室的案例，在这个界面上，有显示聊天内容的文本域，显示输入内容的文本框以及操作的按钮。</p>
<p>给出这样的界面，我们自己就能够看明白，非常的方便直观，这就是使用GUI相关的API来实现的。</p>
<p>而Java为GUI提供的API都存在java.awt和javax.Swing两个包中，我们分别来看一下这两个包：</p>
<ul>
<li><strong>java.awt 包：</strong>
<ul>
<li>awt是这三个单词首字母的缩写，翻译过来是抽象窗口工具包，只不过这个包的API，需要调用本地系统方法实现功能，和本地系统有关联，不利于代码移植，属重量级控件</li>
</ul></li>
<li><strong>javax.swing 包：</strong>
<ul>
<li>它是在awt的基础上，建立的一套图形界面系统，提供了更多的组件，而且完全由Java实现。增强了移植性，属轻量级控件</li>
</ul></li>
</ul>
<p>所以，我们做图形界面开发使用的是swing包下的类。</p>
<p>这里出现了一个名词：组件。那什么是组件呢？</p>
<ul>
<li><strong>组件</strong>是具有图形表示的对象，该图形表示可以显示在屏幕上并且可以与用户交互</li>
</ul>
<p>比如说，在用户登录中，文本框和按钮等，都是能够表示图形的对象，所以它们都称为组件。</p>
<p>最后，我们来说一下，我们会用到的常用组件，</p>
<p>看这里：</p>
<figure>
<img data-src="imgs/1640159115023.png" alt="1640159115023">
<figcaption aria-hidden="true">1640159115023</figcaption>
</figure>
<p><strong>组件：</strong></p>
<ul>
<li><strong>基本组件：</strong>具有图形表示的对象</li>
<li><strong>容器组件：</strong>是可以添加组件的组件。也就是在容器中可以通过add方法添加组件，既可以添加基本组件，也可以添加容器组件。</li>
</ul>
<p>我们先来看常用的基本组件：</p>
<ul>
<li><p>JButton，这是按钮</p></li>
<li><p>JLabel，这是用来显示文本内容的，或者展示图片使用</p></li>
<li><p>JTextFile，这是文本框，用来输入内容的</p></li>
<li><p>JTextArea，这是文本域，用来输入多行多列的数据的</p></li>
</ul>
<p>接着，我们再来看容器组件：</p>
<ul>
<li>面板也是一个容器，我们常用JPanel，后面用到在具体讲解</li>
<li>窗体，下面是Frame，这是awt包下，而我们使用的是JFrame，这是swing包下。</li>
</ul>
<p>看到这里，我要说一下，由于我们使用的都是swing包下的，所以将来使用的组件，都是以J开头的。</p>
<p>好了，GUI相关的基础知识我们就先讲到这里</p>
<h3 id="常用组件">4.2 常用组件</h3>
<h4 id="jframe初识窗体">4.2.1 JFrame(初识窗体)</h4>
<p>来，继续啊，下面我们来学习JFrame窗体类。</p>
<p>在学习JFrame之前，先说一下，在学习GUI相关的API的时候，我就不带着大家到帮助文档中查看了。</p>
<p>在资料中，我们会把要讲解的类的作用，构造方法，和成员方法给大家展示出来，简单的分析后，到IDEA中去实现。</p>
<p>首先，我们来看一下JFrame：</p>
<p><strong>JFrame：</strong></p>
<ul>
<li>是一个顶层窗口</li>
</ul>
<p><strong>构造方法：</strong></p>
<ul>
<li>JFrame()：构造一个最初不可见的新窗体</li>
</ul>
<p><strong>成员方法：</strong></p>
<ul>
<li>void setVisible(boolean b)：显示或隐藏此窗体具体取决于参数b的值</li>
<li>void setSize(int width, int
height)：调整此组件的大小，使其宽度为width，高度为height，单位是像素</li>
</ul>
<p>知道了JFrame的构造方法和成员方法后，我们到IDEA中去使用一下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> javax.swing.*;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    构造方法</span></span><br><span class="line"><span class="comment">        JFrame()：构造一个最初不可见的新窗体</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">    成员方法</span></span><br><span class="line"><span class="comment">        void setVisible(boolean b)：显示或隐藏此窗体具体取决于参数b的值</span></span><br><span class="line"><span class="comment">        void setSize(int width, int height)：调整此组件的大小，使其宽度为width，高度为height，单位是像素</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">JFrameDemo01</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//JFrame()：构造一个最初不可见的新窗体</span></span><br><span class="line">        <span class="type">JFrame</span> <span class="variable">jf</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JFrame</span>();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//void setSize(int width, int height)：调整此组件的大小，使其宽度为width，高度为height，单位是像素</span></span><br><span class="line">        jf.setSize(<span class="number">400</span>,<span class="number">300</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//void setVisible(boolean b)：显示或隐藏此窗体具体取决于参数b的值</span></span><br><span class="line">        jf.setVisible(<span class="literal">true</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>讲解完毕后，大家赶快动手练习一下吧。</p>
<h4 id="jframe常用设置">4.2.2 JFrame(常用设置)</h4>
<p>来，继续啊，下面呢，我们再来学习几个JFrame窗体的常用方法：</p>
<ul>
<li>void setTitle(String title)：设置窗体标题</li>
<li>void setLocationRelativeTo(Component
c)：设置位置，值为null，则窗体位于屏幕中央</li>
<li>void setDefaultCloseOperation(int operation)：设置窗体关闭时默认操作
<ul>
<li>整数3表示：窗口关闭时退出应用程序</li>
</ul></li>
<li>void setAlwaysOnTop(boolean
alwaysOnTop)：设置此窗口是否应始终位于其他窗口之上</li>
</ul>
<p>了解了这几个方法后，我们到IDEA中去使用一下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> javax.swing.*;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    void setTitle(String title)：设置窗体标题</span></span><br><span class="line"><span class="comment">    void setLocationRelativeTo(Component c)：设置位置，值为null，则窗体位于屏幕中央</span></span><br><span class="line"><span class="comment">    void setDefaultCloseOperation(int operation)：设置窗体关闭时默认操作(整数3表示：窗口关闭时退出应用程序)</span></span><br><span class="line"><span class="comment">    void setAlwaysOnTop(boolean alwaysOnTop)：设置此窗口是否应始终位于其他窗口之上</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">JFrameDemo02</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//创建窗体对象</span></span><br><span class="line">        <span class="type">JFrame</span> <span class="variable">jf</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JFrame</span>();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//void setTitle(String title)：设置窗体标题</span></span><br><span class="line">        jf.setTitle(<span class="string">&quot;百度一下，你就知道&quot;</span>);</span><br><span class="line">        <span class="comment">//设置窗体大小</span></span><br><span class="line">        jf.setSize(<span class="number">400</span>, <span class="number">300</span>);</span><br><span class="line">        <span class="comment">//void setDefaultCloseOperation(int operation)：设置窗体关闭时默认操作(整数3表示：窗口关闭时退出应用程序)</span></span><br><span class="line">        jf.setDefaultCloseOperation(<span class="number">3</span>);</span><br><span class="line">        <span class="comment">//void setLocationRelativeTo(Component c)：设置位置，值为null，则窗体位于屏幕中央</span></span><br><span class="line">        jf.setLocationRelativeTo(<span class="literal">null</span>);</span><br><span class="line">        <span class="comment">//void setAlwaysOnTop(boolean alwaysOnTop)：设置此窗口是否应始终位于其他窗口之上</span></span><br><span class="line">        jf.setAlwaysOnTop(<span class="literal">true</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//设置窗体可见</span></span><br><span class="line">        jf.setVisible(<span class="literal">true</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>讲解完毕后，大家赶快动手练习一下吧。</p>
<h4 id="jbutton窗口中添加按钮">4.2.3 JButton(窗口中添加按钮)</h4>
<p>来，继续啊，下面我们来学习JButton，它是按钮的实现。</p>
<p><strong>JButton：</strong></p>
<ul>
<li>按钮的实现</li>
</ul>
<p><strong>构造方法：</strong></p>
<ul>
<li>JButton(String text)：创建一个带文本的按钮</li>
</ul>
<p><strong>成员方法：</strong></p>
<ul>
<li>void setSize(int width, int height)：设置大小</li>
<li>void setLocation(int x, int y)：设置位置(x坐标，y坐标)</li>
</ul>
<p>知道了JButton的构造方法和成员方法后，我们到IDEA中去使用一下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> javax.swing.*;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    构造方法</span></span><br><span class="line"><span class="comment">        JButton(String text)：创建一个带文本的按钮</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">    成员方法</span></span><br><span class="line"><span class="comment">        void setSize(int width, int height)：设置大小</span></span><br><span class="line"><span class="comment">        void setLocation(int x, int y)：设置位置(x坐标，y坐标)</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">JButtonDemo</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//创建窗体对象</span></span><br><span class="line">        <span class="type">JFrame</span> <span class="variable">jf</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JFrame</span>();</span><br><span class="line"></span><br><span class="line">        jf.setTitle(<span class="string">&quot;窗口中添加按钮&quot;</span>);</span><br><span class="line">        jf.setSize(<span class="number">400</span>, <span class="number">300</span>);</span><br><span class="line">        jf.setDefaultCloseOperation(<span class="number">3</span>);</span><br><span class="line">        jf.setLocationRelativeTo(<span class="literal">null</span>);</span><br><span class="line">        jf.setAlwaysOnTop(<span class="literal">true</span>);</span><br><span class="line">        jf.setLayout(<span class="literal">null</span>); <span class="comment">//取消窗体的默认布局</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//JButton(String text)：创建一个带文本的按钮</span></span><br><span class="line">        <span class="type">JButton</span> <span class="variable">btn</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JButton</span>(<span class="string">&quot;我是按钮&quot;</span>);</span><br><span class="line"><span class="comment">//        //void setSize(int width, int height)：设置大小</span></span><br><span class="line"><span class="comment">//        btn.setSize(100,20);</span></span><br><span class="line"><span class="comment">//        //void setLocation(int x, int y)：设置位置(x坐标，y坐标)</span></span><br><span class="line"><span class="comment">////        btn.setLocation(0,0);</span></span><br><span class="line"><span class="comment">//        btn.setLocation(100,100);</span></span><br><span class="line">        btn.setBounds(<span class="number">100</span>,<span class="number">100</span>,<span class="number">100</span>,<span class="number">20</span>);</span><br><span class="line"></span><br><span class="line">        <span class="type">JButton</span> <span class="variable">btn2</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JButton</span>(<span class="string">&quot;我是按钮2&quot;</span>);</span><br><span class="line">        btn2.setBounds(<span class="number">100</span>,<span class="number">120</span>,<span class="number">100</span>,<span class="number">20</span>);</span><br><span class="line"></span><br><span class="line">        jf.add(btn);</span><br><span class="line">        jf.add(btn2);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//设置窗体可见</span></span><br><span class="line">        jf.setVisible(<span class="literal">true</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>演示完毕之后，回到资料再总结一下：</p>
<p><strong>JButton：</strong></p>
<ul>
<li>按钮的实现</li>
</ul>
<p><strong>构造方法：</strong></p>
<ul>
<li>JButton(String text)：创建一个带文本的按钮</li>
</ul>
<p><strong>成员方法：</strong></p>
<ul>
<li><p>void setSize(int width, int height)：设置大小</p></li>
<li><p>void setLocation(int x, int y)：设置位置(x坐标，y坐标)</p></li>
<li><p><font color="red"><strong>void setBounds(int x, int y, int width,
int height)：设置位置和大小</strong></font></p></li>
</ul>
<p><strong>和窗体相关操作：</strong></p>
<ul>
<li>取消窗体默认布局：<font color="red"><strong>窗体对象.setLayout(null);</strong></font></li>
<li>把按钮添加到窗体：<font color="red"><strong>窗体对象.add(按钮对象);</strong></font></li>
</ul>
<p>讲解完毕后，大家赶快动手练习一下吧。</p>
<h4 id="jlabel显示文本和图像">4.2.4 JLabel(显示文本和图像)</h4>
<p>来，继续啊，下面呢，我们来学习JLable，它是用来做短文本字符串或图像的显示的。</p>
<p><strong>JLable：</strong></p>
<ul>
<li>短文本字符串或图像的显示区域</li>
</ul>
<p><strong>构造方法：</strong></p>
<ul>
<li>JLabel(String text)：使用指定的文本创建 JLabel实例</li>
<li>JLabel(Icon image)：使用指定的图像创建 JLabel实例
<ul>
<li>ImageIcon(String filename)：从指定的文件创建ImageIcon</li>
<li>文件路径：绝对路径和相对路径</li>
<li>绝对路径：完整的路径名，不需要任何其他信息就可以定位它所表示的文件
<ul>
<li>例如：D:_code-api-gui.png</li>
</ul></li>
<li>相对路径：必须使用取自其他路径名的信息进行解释
<ul>
<li>例如：itheima-api-gui.png</li>
</ul></li>
</ul></li>
</ul>
<p><strong>成员方法：</strong></p>
<ul>
<li>void setBounds(int x, int y, int width, int
height)：设置位置和大小</li>
</ul>
<p>知道了JLabel的构造方法和成员方法后，我们到IDEA中去使用一下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> javax.swing.*;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    构造方法</span></span><br><span class="line"><span class="comment">        JLabel(String text)：使用指定的文本创建 JLabel实例</span></span><br><span class="line"><span class="comment">        JLabel(Icon image)：使用指定的图像创建 JLabel实例</span></span><br><span class="line"><span class="comment">            ImageIcon(String filename)：从指定的文件创建ImageIcon</span></span><br><span class="line"><span class="comment">    成员方法</span></span><br><span class="line"><span class="comment">        void setBounds(int x, int y, int width, int height)：设置位置和大小</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">JLabelDemo</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//创建窗体对象</span></span><br><span class="line">        <span class="type">JFrame</span> <span class="variable">jf</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JFrame</span>();</span><br><span class="line"></span><br><span class="line">        jf.setTitle(<span class="string">&quot;显示文本和图像&quot;</span>);</span><br><span class="line">        jf.setSize(<span class="number">400</span>, <span class="number">300</span>);</span><br><span class="line">        jf.setDefaultCloseOperation(<span class="number">3</span>);</span><br><span class="line">        jf.setLocationRelativeTo(<span class="literal">null</span>);</span><br><span class="line">        jf.setAlwaysOnTop(<span class="literal">true</span>);</span><br><span class="line">        jf.setLayout(<span class="literal">null</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//JLabel(String text)：使用指定的文本创建 JLabel实例</span></span><br><span class="line">        <span class="type">JLabel</span> <span class="variable">jLabel</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JLabel</span>(<span class="string">&quot;好好学习&quot;</span>);</span><br><span class="line">        jLabel.setBounds(<span class="number">0</span>,<span class="number">0</span>,<span class="number">100</span>,<span class="number">20</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//JLabel(Icon image)：使用指定的图像创建 JLabel实例</span></span><br><span class="line">        <span class="comment">//ImageIcon(String filename)：从指定的文件创建ImageIcon</span></span><br><span class="line">        <span class="comment">//D:\IdeaProjects\javase_code\itheima-api-gui\images\mn.png</span></span><br><span class="line"><span class="comment">//        ImageIcon imageIcon = new ImageIcon(&quot;D:\\IdeaProjects\\javase_code\\itheima-api-gui\\images\\mn.png&quot;);</span></span><br><span class="line"><span class="comment">//        JLabel jLabel2 = new JLabel(imageIcon);</span></span><br><span class="line"><span class="comment">//        JLabel jLabel2 = new JLabel(new ImageIcon(&quot;D:\\IdeaProjects\\javase_code\\itheima-api-gui\\images\\mn.png&quot;));</span></span><br><span class="line">        <span class="type">JLabel</span> <span class="variable">jLabel2</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JLabel</span>(<span class="keyword">new</span> <span class="title class_">ImageIcon</span>(<span class="string">&quot;itheima-api-gui\\images\\mn.png&quot;</span>));</span><br><span class="line">        jLabel2.setBounds(<span class="number">50</span>,<span class="number">50</span>,<span class="number">100</span>,<span class="number">143</span>);</span><br><span class="line"></span><br><span class="line">        jf.add(jLabel);</span><br><span class="line">        jf.add(jLabel2);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//设置窗体可见</span></span><br><span class="line">        jf.setVisible(<span class="literal">true</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>讲解完毕后，大家赶快动手练习一下吧。</p>
<p><font color="red"><strong>注意：</strong></font>在实际开发中，大家不用担心每个组件的位置和大小。因为这些都是前端人员设计好的。</p>
<h3 id="案例">4.3 案例</h3>
<p>来，继续啊，下面呢，我们通过几个案例，来使用一下GUI中的常用组件。目前呢，我们仅仅是把界面做出来，下一次课呢，我们就要把最终的功能给实现了。首先，我们来看第一个案例。</p>
<h4 id="案例1用户登录">4.3.1 案例1(用户登录)</h4>
<p>需求：如图所示，做出界面</p>
<p>提示：给出的数据就是按照组件位置和大小给出的</p>
<figure>
<img data-src="imgs/1640160198618.png" alt="1640160198618">
<figcaption aria-hidden="true">1640160198618</figcaption>
</figure>
<p>首先，我们来简单的分析一下：</p>
<p><strong>分析：</strong></p>
<p>① 2个JLabel</p>
<p>② 2个JTextField</p>
<p>③ 1个JButton</p>
<p>分析完毕后，我们到IDEA中去实现一下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> javax.swing.*;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    用户登录</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">UserLogin</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//创建窗体对象</span></span><br><span class="line">        <span class="type">JFrame</span> <span class="variable">jf</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JFrame</span>();</span><br><span class="line"></span><br><span class="line">        jf.setTitle(<span class="string">&quot;用户登录&quot;</span>);</span><br><span class="line">        jf.setSize(<span class="number">400</span>, <span class="number">300</span>);</span><br><span class="line">        jf.setDefaultCloseOperation(<span class="number">3</span>);</span><br><span class="line">        jf.setLocationRelativeTo(<span class="literal">null</span>);</span><br><span class="line">        jf.setAlwaysOnTop(<span class="literal">true</span>);</span><br><span class="line">        jf.setLayout(<span class="literal">null</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//显示用户名文本</span></span><br><span class="line">        <span class="type">JLabel</span> <span class="variable">usernameLable</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JLabel</span>(<span class="string">&quot;用户名&quot;</span>);</span><br><span class="line">        usernameLable.setBounds(<span class="number">50</span>,<span class="number">50</span>,<span class="number">50</span>,<span class="number">20</span>);</span><br><span class="line">        jf.add(usernameLable);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//用户名输入框</span></span><br><span class="line">        <span class="type">JTextField</span> <span class="variable">usernameField</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JTextField</span>();</span><br><span class="line">        usernameField.setBounds(<span class="number">150</span>,<span class="number">50</span>,<span class="number">180</span>,<span class="number">20</span>);</span><br><span class="line">        jf.add(usernameField);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//显示密码文本</span></span><br><span class="line">        <span class="type">JLabel</span> <span class="variable">passwordLable</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JLabel</span>(<span class="string">&quot;密码&quot;</span>);</span><br><span class="line">        passwordLable.setBounds(<span class="number">50</span>,<span class="number">100</span>,<span class="number">50</span>,<span class="number">20</span>);</span><br><span class="line">        jf.add(passwordLable);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//密码输入框</span></span><br><span class="line"><span class="comment">//        JTextField passwordField = new JTextField();</span></span><br><span class="line"><span class="comment">//        passwordField.setBounds(150,100,180,20);</span></span><br><span class="line"><span class="comment">//        jf.add(passwordField);</span></span><br><span class="line"></span><br><span class="line">        <span class="type">JPasswordField</span> <span class="variable">passwordField</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JPasswordField</span>();</span><br><span class="line">        passwordField.setBounds(<span class="number">150</span>,<span class="number">100</span>,<span class="number">180</span>,<span class="number">20</span>);</span><br><span class="line">        jf.add(passwordField);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//登录按钮</span></span><br><span class="line">        <span class="type">JButton</span> <span class="variable">loginButton</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JButton</span>(<span class="string">&quot;登录&quot;</span>);</span><br><span class="line">        loginButton.setBounds(<span class="number">50</span>,<span class="number">200</span>,<span class="number">280</span>,<span class="number">20</span>);</span><br><span class="line">        jf.add(loginButton);</span><br><span class="line"></span><br><span class="line">        jf.setVisible(<span class="literal">true</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>在讲解的过程中，引入了一个新的组件JPasswordField，用来表示密码框。</p>
<p>讲解完毕后，大家赶快动手练习一下吧。</p>
<h4 id="案例2聊天室">4.3.2 案例2(聊天室)</h4>
<p>需求：如图所示，做出界面</p>
<p>提示：给出的数据就是按照组件位置和大小给出的</p>
<figure>
<img data-src="imgs/1640160345308.png" alt="1640160345308">
<figcaption aria-hidden="true">1640160345308</figcaption>
</figure>
<p>首先，我们来简单的分析一下：</p>
<p><strong>分析：</strong></p>
<p>① 1个JTextArea</p>
<p>② 1个JTextField</p>
<p>③ 2个JButton</p>
<p>分析完毕后，我们到IDEA中去实现一下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> javax.swing.*;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    聊天室</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">ChatRoom</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//创建窗体对象</span></span><br><span class="line">        <span class="type">JFrame</span> <span class="variable">jf</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JFrame</span>();</span><br><span class="line"></span><br><span class="line">        jf.setTitle(<span class="string">&quot;聊天室&quot;</span>);</span><br><span class="line">        jf.setSize(<span class="number">400</span>, <span class="number">300</span>);</span><br><span class="line">        jf.setDefaultCloseOperation(<span class="number">3</span>);</span><br><span class="line">        jf.setLocationRelativeTo(<span class="literal">null</span>);</span><br><span class="line">        jf.setAlwaysOnTop(<span class="literal">true</span>);</span><br><span class="line">        jf.setLayout(<span class="literal">null</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//显示聊天信息的文本域</span></span><br><span class="line">        <span class="type">JTextArea</span> <span class="variable">messageArea</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JTextArea</span>();</span><br><span class="line">        messageArea.setBounds(<span class="number">10</span>,<span class="number">10</span>,<span class="number">360</span>,<span class="number">200</span>);</span><br><span class="line">        jf.add(messageArea);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//输入聊天信息的文本框</span></span><br><span class="line">        <span class="type">JTextField</span> <span class="variable">messageField</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JTextField</span>();</span><br><span class="line">        messageField.setBounds(<span class="number">10</span>,<span class="number">230</span>,<span class="number">180</span>,<span class="number">20</span>);</span><br><span class="line">        jf.add(messageField);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//发送按钮</span></span><br><span class="line">        <span class="type">JButton</span> <span class="variable">sendButton</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JButton</span>(<span class="string">&quot;发送&quot;</span>);</span><br><span class="line">        sendButton.setBounds(<span class="number">200</span>,<span class="number">230</span>,<span class="number">70</span>,<span class="number">20</span>);</span><br><span class="line">        jf.add(sendButton);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//清空聊天按钮</span></span><br><span class="line">        <span class="type">JButton</span> <span class="variable">clearButton</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JButton</span>(<span class="string">&quot;清空聊天&quot;</span>);</span><br><span class="line">        clearButton.setBounds(<span class="number">280</span>,<span class="number">230</span>,<span class="number">100</span>,<span class="number">20</span>);</span><br><span class="line">        jf.add(clearButton);</span><br><span class="line"></span><br><span class="line">        jf.setVisible(<span class="literal">true</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>讲解完毕后，大家赶快动手练习一下吧。</p>
<h4 id="案例3猜数字">4.3.3 案例3(猜数字)</h4>
<p>需求：如图所示，做出界面</p>
<p>提示：给出的数据就是按照组件位置和大小给出的</p>
<figure>
<img data-src="imgs/1640160399176.png" alt="1640160399176">
<figcaption aria-hidden="true">1640160399176</figcaption>
</figure>
<p>首先，我们来简单的分析一下：</p>
<p><strong>分析：</strong></p>
<p>① 1个JLabel</p>
<p>② 1个JTextField</p>
<p>③ 1个JButton</p>
<p>分析完毕后，我们到IDEA中去实现一下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> javax.swing.*;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    猜数字</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">GuessNumber</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//创建窗体对象</span></span><br><span class="line">        <span class="type">JFrame</span> <span class="variable">jf</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JFrame</span>();</span><br><span class="line"></span><br><span class="line">        jf.setTitle(<span class="string">&quot;猜数字&quot;</span>);</span><br><span class="line">        jf.setSize(<span class="number">400</span>, <span class="number">300</span>);</span><br><span class="line">        jf.setDefaultCloseOperation(<span class="number">3</span>);</span><br><span class="line">        jf.setLocationRelativeTo(<span class="literal">null</span>);</span><br><span class="line">        jf.setAlwaysOnTop(<span class="literal">true</span>);</span><br><span class="line">        jf.setLayout(<span class="literal">null</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//提示信息</span></span><br><span class="line">        <span class="type">JLabel</span> <span class="variable">messageLable</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JLabel</span>(<span class="string">&quot;系统产生了一个1-100之间的数据，请猜一猜&quot;</span>);</span><br><span class="line">        messageLable.setBounds(<span class="number">70</span>,<span class="number">50</span>,<span class="number">350</span>,<span class="number">20</span>);</span><br><span class="line">        jf.add(messageLable);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//输入要猜的数字</span></span><br><span class="line">        <span class="type">JTextField</span> <span class="variable">numberField</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JTextField</span>();</span><br><span class="line">        numberField.setBounds(<span class="number">120</span>,<span class="number">100</span>,<span class="number">150</span>,<span class="number">20</span>);</span><br><span class="line">        jf.add(numberField);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//猜数字的按钮</span></span><br><span class="line">        <span class="type">JButton</span> <span class="variable">guessButton</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JButton</span>(<span class="string">&quot;我猜&quot;</span>);</span><br><span class="line">        guessButton.setBounds(<span class="number">150</span>,<span class="number">150</span>,<span class="number">100</span>,<span class="number">20</span>);</span><br><span class="line">        jf.add(guessButton);</span><br><span class="line"></span><br><span class="line">        jf.setVisible(<span class="literal">true</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>讲解完毕后，大家赶快动手练习一下吧。</p>
<h4 id="案例4手机日期和时间显示">4.3.4 案例4(手机日期和时间显示)</h4>
<p>需求：如图所示，做出界面</p>
<p>提示：给出的数据就是按照组件位置和大小给出的</p>
<p>参照图：</p>
<figure>
<img data-src="imgs/1640160482727.png" alt="1640160482727">
<figcaption aria-hidden="true">1640160482727</figcaption>
</figure>
<p>我们要实现的图：</p>
<figure>
<img data-src="imgs/1640160461955.png" alt="1640160461955">
<figcaption aria-hidden="true">1640160461955</figcaption>
</figure>
<p>首先，我们来简单的分析一下：</p>
<p><strong>分析：</strong></p>
<p>① 4个JLabel</p>
<p>分析完毕后，我们到IDEA中去实现一下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> javax.swing.*;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    手机日期和时间显示</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">ShowDateTime</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//创建窗体对象</span></span><br><span class="line">        <span class="type">JFrame</span> <span class="variable">jf</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JFrame</span>();</span><br><span class="line"></span><br><span class="line">        jf.setTitle(<span class="string">&quot;手机日期和时间显示&quot;</span>);</span><br><span class="line">        jf.setSize(<span class="number">400</span>, <span class="number">300</span>);</span><br><span class="line">        jf.setDefaultCloseOperation(<span class="number">3</span>);</span><br><span class="line">        jf.setLocationRelativeTo(<span class="literal">null</span>);</span><br><span class="line">        jf.setAlwaysOnTop(<span class="literal">true</span>);</span><br><span class="line">        jf.setLayout(<span class="literal">null</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//提示日期</span></span><br><span class="line">        <span class="type">JLabel</span> <span class="variable">dateLable</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JLabel</span>(<span class="string">&quot;日期&quot;</span>);</span><br><span class="line">        dateLable.setBounds(<span class="number">50</span>,<span class="number">50</span>,<span class="number">100</span>,<span class="number">20</span>);</span><br><span class="line">        jf.add(dateLable);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//按照格式显示日期的字符串</span></span><br><span class="line">        <span class="type">JLabel</span> <span class="variable">showDateLable</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JLabel</span>(<span class="string">&quot;xxxx年xx月xx日&quot;</span>);</span><br><span class="line">        showDateLable.setBounds(<span class="number">50</span>,<span class="number">80</span>,<span class="number">200</span>,<span class="number">20</span>);</span><br><span class="line">        jf.add(showDateLable);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//提示时间</span></span><br><span class="line">        <span class="type">JLabel</span> <span class="variable">timeLable</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JLabel</span>(<span class="string">&quot;时间&quot;</span>);</span><br><span class="line">        timeLable.setBounds(<span class="number">50</span>,<span class="number">150</span>,<span class="number">100</span>,<span class="number">20</span>);</span><br><span class="line">        jf.add(timeLable);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//按照格式显示时间的字符串</span></span><br><span class="line">        <span class="type">JLabel</span> <span class="variable">showTimeLable</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JLabel</span>(<span class="string">&quot;xx:xx&quot;</span>);</span><br><span class="line">        showTimeLable.setBounds(<span class="number">50</span>,<span class="number">180</span>,<span class="number">200</span>,<span class="number">20</span>);</span><br><span class="line">        jf.add(showTimeLable);</span><br><span class="line"></span><br><span class="line">        jf.setVisible(<span class="literal">true</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>讲解完毕后，大家赶快动手练习一下吧。</p>
<h4 id="案例5考勤查询">4.3.5 案例5(考勤查询)</h4>
<p>需求：如图所示，做出界面</p>
<p>提示：给出的数据就是按照组件位置和大小给出的</p>
<figure>
<img data-src="imgs/1640160575426.png" alt="1640160575426">
<figcaption aria-hidden="true">1640160575426</figcaption>
</figure>
<p>首先，我们来简单的分析一下：</p>
<p><strong>分析：</strong></p>
<p>① 3个JLabel</p>
<p>② 2个JTextField</p>
<p>③ 1个JButton</p>
<p>分析完毕后，我们到IDEA中去实现一下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> javax.swing.*;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    考勤查询</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">AttendanceQuery01</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//创建窗体对象</span></span><br><span class="line">        <span class="type">JFrame</span> <span class="variable">jf</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JFrame</span>();</span><br><span class="line"></span><br><span class="line">        jf.setTitle(<span class="string">&quot;考勤查询&quot;</span>);</span><br><span class="line">        jf.setSize(<span class="number">400</span>, <span class="number">300</span>);</span><br><span class="line">        jf.setDefaultCloseOperation(<span class="number">3</span>);</span><br><span class="line">        jf.setLocationRelativeTo(<span class="literal">null</span>);</span><br><span class="line">        jf.setAlwaysOnTop(<span class="literal">true</span>);</span><br><span class="line">        jf.setLayout(<span class="literal">null</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//显示考勤日期的文本</span></span><br><span class="line">        <span class="type">JLabel</span> <span class="variable">dateLable</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JLabel</span>(<span class="string">&quot;考勤日期&quot;</span>);</span><br><span class="line">        dateLable.setBounds(<span class="number">50</span>,<span class="number">20</span>,<span class="number">100</span>,<span class="number">20</span>);</span><br><span class="line">        jf.add(dateLable);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//显示开始时间文本</span></span><br><span class="line">        <span class="type">JLabel</span> <span class="variable">startDateLable</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JLabel</span>(<span class="string">&quot;开始时间&quot;</span>);</span><br><span class="line">        startDateLable.setBounds(<span class="number">50</span>,<span class="number">70</span>,<span class="number">100</span>,<span class="number">20</span>);</span><br><span class="line">        jf.add(startDateLable);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//开始时间输入框</span></span><br><span class="line">        <span class="type">JTextField</span> <span class="variable">startDateField</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JTextField</span>();</span><br><span class="line">        startDateField.setBounds(<span class="number">50</span>,<span class="number">100</span>,<span class="number">100</span>,<span class="number">20</span>);</span><br><span class="line">        jf.add(startDateField);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//显示结束时间文本</span></span><br><span class="line">        <span class="type">JLabel</span> <span class="variable">endDateLable</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JLabel</span>(<span class="string">&quot;结束时间&quot;</span>);</span><br><span class="line">        endDateLable.setBounds(<span class="number">250</span>,<span class="number">70</span>,<span class="number">100</span>,<span class="number">20</span>);</span><br><span class="line">        jf.add(endDateLable);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//结束时间输入框</span></span><br><span class="line">        <span class="type">JTextField</span> <span class="variable">endDateField</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JTextField</span>();</span><br><span class="line">        endDateField.setBounds(<span class="number">250</span>,<span class="number">100</span>,<span class="number">100</span>,<span class="number">20</span>);</span><br><span class="line">        jf.add(endDateField);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//确定按钮</span></span><br><span class="line">        <span class="type">JButton</span> <span class="variable">confirmButton</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JButton</span>(<span class="string">&quot;确定&quot;</span>);</span><br><span class="line">        confirmButton.setBounds(<span class="number">250</span>,<span class="number">180</span>,<span class="number">60</span>,<span class="number">20</span>);</span><br><span class="line">        jf.add(confirmButton);</span><br><span class="line"></span><br><span class="line">        jf.setVisible(<span class="literal">true</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>讲解完毕后，大家赶快动手练习一下吧。</p>
<h4 id="案例5考勤查询之日历控件">4.3.6 案例5(考勤查询之日历控件)</h4>
<p>需求：用日历控件改进考勤查询的日期字符串填写</p>
<p>来，继续啊，下面我们来学习考勤查询之日历控件</p>
<p>首先，我们看一下需求：用日历控件改进考勤查询的日期字符串填写</p>
<p>什么意思呢？我们刚在做的考勤查询系统，是需要手动输入日期字符串的，而在实际开发中，我们一般会选择使用日历控件来实现，</p>
<p>也就是下面这个样子的，我们点击一下，就会出现日历控件，我们选择日期就可以了。</p>
<figure>
<img data-src="imgs/1640160765732.png" alt="1640160765732">
<figcaption aria-hidden="true">1640160765732</figcaption>
</figure>
<p>这个相信大家有见过。</p>
<p>知道了要做什么之后，下面我们来说一下，怎么做。</p>
<p>我们可以自己写这个日历控件，也可以使用别人写好的日历控件。鉴于目前所学知识有限，我们先使用别人提供的日历控件。</p>
<p>这里呢，我准备好了一个<strong>日历控件类(DateChooser.java)</strong>，我们到IDEA中一起去看一下如何使用：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">    <span class="comment">//创建窗体</span></span><br><span class="line">    <span class="type">JFrame</span> <span class="variable">jf</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JFrame</span>();</span><br><span class="line"></span><br><span class="line">    <span class="comment">//设置窗体大小</span></span><br><span class="line">    jf.setSize(<span class="number">400</span>, <span class="number">300</span>);</span><br><span class="line">    <span class="comment">//设置窗体标题</span></span><br><span class="line">    jf.setTitle(<span class="string">&quot;日历控件&quot;</span>);</span><br><span class="line">    <span class="comment">//设置位置，值为null，则窗体位于屏幕中央</span></span><br><span class="line">    jf.setLocationRelativeTo(<span class="literal">null</span>);</span><br><span class="line">    <span class="comment">//设置窗体关闭时默认操作，窗口关闭时退出应用程序</span></span><br><span class="line">    jf.setDefaultCloseOperation(<span class="number">3</span>);</span><br><span class="line">    <span class="comment">//设置此窗口是否应始终位于其他窗口之上</span></span><br><span class="line">    jf.setAlwaysOnTop(<span class="literal">true</span>);</span><br><span class="line">    <span class="comment">//取消窗体默认布局：窗体对象.setLayout(null);</span></span><br><span class="line">    jf.setLayout(<span class="literal">null</span>);</span><br><span class="line"></span><br><span class="line">    <span class="comment">//创建日期选择器对象，指定日期字符串格式</span></span><br><span class="line">    <span class="type">DateChooser</span> <span class="variable">dateChooser</span> <span class="operator">=</span> DateChooser.getInstance(<span class="string">&quot;yyyy-MM-dd&quot;</span>);</span><br><span class="line"></span><br><span class="line">    <span class="type">JTextField</span> <span class="variable">showDateField</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JTextField</span>(<span class="string">&quot;单击选择日期&quot;</span>);</span><br><span class="line">    showDateField.setBounds(<span class="number">50</span>, <span class="number">50</span>, <span class="number">100</span>, <span class="number">20</span>);</span><br><span class="line"></span><br><span class="line">    <span class="comment">//把日历控件和文本框进行绑定</span></span><br><span class="line">    dateChooser.register(showDateField);</span><br><span class="line"></span><br><span class="line">    jf.add(showDateField);</span><br><span class="line"></span><br><span class="line">    jf.setVisible(<span class="literal">true</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>了解了如何使用日历控件后，我们把开始写的代码改进一下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> javax.swing.*;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    考勤查询</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">AttendanceQuery02</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//创建窗体对象</span></span><br><span class="line">        <span class="type">JFrame</span> <span class="variable">jf</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JFrame</span>();</span><br><span class="line"></span><br><span class="line">        jf.setTitle(<span class="string">&quot;考勤查询&quot;</span>);</span><br><span class="line">        jf.setSize(<span class="number">400</span>, <span class="number">300</span>);</span><br><span class="line">        jf.setDefaultCloseOperation(<span class="number">3</span>);</span><br><span class="line">        jf.setLocationRelativeTo(<span class="literal">null</span>);</span><br><span class="line">        jf.setAlwaysOnTop(<span class="literal">true</span>);</span><br><span class="line">        jf.setLayout(<span class="literal">null</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//显示考勤日期的文本</span></span><br><span class="line">        <span class="type">JLabel</span> <span class="variable">dateLable</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JLabel</span>(<span class="string">&quot;考勤日期&quot;</span>);</span><br><span class="line">        dateLable.setBounds(<span class="number">50</span>,<span class="number">20</span>,<span class="number">100</span>,<span class="number">20</span>);</span><br><span class="line">        jf.add(dateLable);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//显示开始时间文本</span></span><br><span class="line">        <span class="type">JLabel</span> <span class="variable">startDateLable</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JLabel</span>(<span class="string">&quot;开始时间&quot;</span>);</span><br><span class="line">        startDateLable.setBounds(<span class="number">50</span>,<span class="number">70</span>,<span class="number">100</span>,<span class="number">20</span>);</span><br><span class="line">        jf.add(startDateLable);</span><br><span class="line"></span><br><span class="line">        <span class="type">DateChooser</span> <span class="variable">dateChooser1</span> <span class="operator">=</span> DateChooser.getInstance(<span class="string">&quot;yyyy/MM/dd&quot;</span>);</span><br><span class="line">        <span class="type">DateChooser</span> <span class="variable">dateChooser2</span> <span class="operator">=</span> DateChooser.getInstance(<span class="string">&quot;yyyy/MM/dd&quot;</span>);</span><br><span class="line">        <span class="comment">//开始时间输入框</span></span><br><span class="line">        <span class="type">JTextField</span> <span class="variable">startDateField</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JTextField</span>();</span><br><span class="line">        startDateField.setBounds(<span class="number">50</span>,<span class="number">100</span>,<span class="number">100</span>,<span class="number">20</span>);</span><br><span class="line">        dateChooser1.register(startDateField);</span><br><span class="line">        jf.add(startDateField);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//显示结束时间文本</span></span><br><span class="line">        <span class="type">JLabel</span> <span class="variable">endDateLable</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JLabel</span>(<span class="string">&quot;结束时间&quot;</span>);</span><br><span class="line">        endDateLable.setBounds(<span class="number">250</span>,<span class="number">70</span>,<span class="number">100</span>,<span class="number">20</span>);</span><br><span class="line">        jf.add(endDateLable);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//结束时间输入框</span></span><br><span class="line">        <span class="type">JTextField</span> <span class="variable">endDateField</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JTextField</span>();</span><br><span class="line">        endDateField.setBounds(<span class="number">250</span>,<span class="number">100</span>,<span class="number">100</span>,<span class="number">20</span>);</span><br><span class="line">        dateChooser2.register(endDateField);</span><br><span class="line">        jf.add(endDateField);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//确定按钮</span></span><br><span class="line">        <span class="type">JButton</span> <span class="variable">confirmButton</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JButton</span>(<span class="string">&quot;确定&quot;</span>);</span><br><span class="line">        confirmButton.setBounds(<span class="number">250</span>,<span class="number">180</span>,<span class="number">60</span>,<span class="number">20</span>);</span><br><span class="line">        jf.add(confirmButton);</span><br><span class="line"></span><br><span class="line">        jf.setVisible(<span class="literal">true</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>讲解完毕后，大家赶快动手练习一下吧。</p>
<h3 id="事件监听机制">4.4 事件监听机制</h3>
<p>来，继续啊，目前呢，我们做好了几个GUI的界面，但是具体的功能，我们没法实现。因为要想实现功能，就必须要学习一下事件监听机制。</p>
<p>这里，我们一起来说一下<strong>事件监听机制的组成：</strong></p>
<ul>
<li><strong>事件源：</strong>事件发生的地方。可以是按钮，窗体，图片等</li>
<li><strong>事件：</strong>发生了什么事情。例如：鼠标点击事件，键盘按下事件等</li>
<li><strong>事件绑定：</strong>把事件绑定到事件源上，当发生了某个事件，则触发对应的处理逻辑
<ul>
<li>事件源对象. addXXXListener(事件);</li>
</ul></li>
</ul>
<p>GUI中的事件比较多，这里呢，我们先告诉一下大家如何给按钮添加事件，能够把我们前面的案例给实现就可以了。至于其他事件，后面我们用到的时候再去学习。</p>
<figure>
<img data-src="imgs/1640161060785.png" alt="1640161060785">
<figcaption aria-hidden="true">1640161060785</figcaption>
</figure>
<p>了解了如何给按钮添加事件后，我们到IDEA中去体验一下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> javax.swing.*;</span><br><span class="line"><span class="keyword">import</span> java.awt.event.ActionEvent;</span><br><span class="line"><span class="keyword">import</span> java.awt.event.ActionListener;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    事件监听机制</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">ActionListenerDemo</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//创建窗体对象</span></span><br><span class="line">        <span class="type">JFrame</span> <span class="variable">jf</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JFrame</span>();</span><br><span class="line"></span><br><span class="line">        jf.setTitle(<span class="string">&quot;事件监听机制&quot;</span>);</span><br><span class="line">        jf.setSize(<span class="number">400</span>, <span class="number">300</span>);</span><br><span class="line">        jf.setDefaultCloseOperation(<span class="number">3</span>);</span><br><span class="line">        jf.setLocationRelativeTo(<span class="literal">null</span>);</span><br><span class="line">        jf.setAlwaysOnTop(<span class="literal">true</span>);</span><br><span class="line">        jf.setLayout(<span class="literal">null</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//创建按钮</span></span><br><span class="line">        <span class="type">JButton</span> <span class="variable">jButton</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JButton</span>(<span class="string">&quot;你点我啊&quot;</span>);</span><br><span class="line">        jButton.setBounds(<span class="number">0</span>, <span class="number">0</span>, <span class="number">100</span>, <span class="number">100</span>);</span><br><span class="line">        jf.add(jButton);</span><br><span class="line"></span><br><span class="line">        jButton.addActionListener(<span class="keyword">new</span> <span class="title class_">ActionListener</span>() &#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">actionPerformed</span><span class="params">(ActionEvent e)</span> &#123;</span><br><span class="line">                System.out.println(<span class="string">&quot;你点我啊&quot;</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line"></span><br><span class="line">        jf.setVisible(<span class="literal">true</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>讲解完毕后，大家赶快动手练习一下吧。</p>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="reward-container">
  <div>请我一杯咖啡吧！</div>
  <button>
    赞赏
  </button>
  <div class="post-reward">
      <div>
        <img src="/images/wChat.png" alt="Ethan Liu 微信">
        <span>微信</span>
      </div>

  </div>
</div>

          

<div class="post-copyright">
<ul>
  <li class="post-copyright-author">
      <strong>本文作者： </strong>Ethan Liu
  </li>
  <li class="post-copyright-link">
      <strong>本文链接：</strong>
      <a href="http://ethanliu6.github.io/2024/03/19/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day06%E5%B8%B8%E7%94%A8API(1)/" title="JavaSE基础——Day06 JavaSE基础——常用API:Scanner、Random、GUI">http://ethanliu6.github.io/2024/03/19/JavaNotes/JavaSE基础/day06常用API(1)/</a>
  </li>
  <li class="post-copyright-license">
      <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <span class="exturl" data-url="aHR0cHM6Ly9jcmVhdGl2ZWNvbW1vbnMub3JnL2xpY2Vuc2VzL2J5LW5jLXNhLzQuMC8="><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</span> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


          <div class="post-tags">
              <a href="/tags/Java/" rel="tag"><i class="fa fa-tag"></i> Java</a>
              <a href="/tags/Java%E5%9F%BA%E7%A1%80/" rel="tag"><i class="fa fa-tag"></i> Java基础</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2024/03/17/JavaNotes/MySQL/day04/C3P0/" rel="prev" title="">
                  <i class="fa fa-angle-left"></i> 
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2024/03/21/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day07%E5%B8%B8%E7%94%A8API(2)/" rel="next" title="JavaSE基础——Day07 JavaSE基础——常用API:String、Integer、Date、SDF">
                  JavaSE基础——Day07 JavaSE基础——常用API:String、Integer、Date、SDF <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






    <div class="comments" id="SOHUCS" sid="310206cc29688fe458e006a7c7db7957"></div>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2023-2024 – 
    <span itemprop="copyrightYear">2024</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">Is Ethan's</span>
  </div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
    <span title="站点总字数">113k</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">6:51</span>
  </span>
</div>
<div class="busuanzi-count">
    <span class="post-meta-item" id="busuanzi_container_site_uv">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-item" id="busuanzi_container_site_pv">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>

    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>

  <span class="exturl github-corner" data-url="aHR0cHM6Ly9naXRodWIuY29tL0V0aGFuTGl1Ni8=" title="在 GitHub 上关注我" aria-label="在 GitHub 上关注我"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></span>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.33/fancybox/fancybox.umd.js" integrity="sha256-+2+qOqR8CKoHh/AsVR9k2qaDBKWjYNC2nozhYmv5j9k=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/medium-zoom/1.1.0/medium-zoom.min.js" integrity="sha256-ZgMyDAIYDYGxbcpJcfUnYwNevG/xi9OHKaR/8GK+jWc=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/bookmark.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>

  <script class="next-config" data-name="pdf" type="application/json">{"object_url":{"url":"https://cdnjs.cloudflare.com/ajax/libs/pdfobject/2.2.12/pdfobject.min.js","integrity":"sha256-g2xji1rlE3KsGVClvuxTbcR0Kn2+wtQADSff2Tbb4zA="},"url":"/lib/pdf/web/viewer.html"}</script>
  <script src="/js/third-party/tags/pdf.js"></script>



  <script src="/js/third-party/fancybox.js"></script>



  
  <script data-pjax async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>




  

  <script class="next-config" data-name="enableMath" type="application/json">true</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"all","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


  <script src="https://cdnjs.cloudflare.com/ajax/libs/quicklink/2.3.0/quicklink.umd.js" integrity="sha256-yvJQOINiH9fWemHn0vCA5lsHWJaHs6/ZmO+1Ft04SvM=" crossorigin="anonymous"></script>
  <script class="next-config" data-name="quicklink" type="application/json">{"enable":true,"home":true,"archive":true,"delay":true,"timeout":3000,"priority":true,"url":"http://ethanliu6.github.io/2024/03/19/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day06%E5%B8%B8%E7%94%A8API(1)/"}</script>
  <script src="/js/third-party/quicklink.js"></script>
<script class="next-config" data-name="changyan" type="application/json">{"enable":true,"appid":"cyxf6tytI","appkey":"90987f30c4b5248baad5a04c7a6abfe1","count":true}</script>
<script src="/js/third-party/comments/changyan.js"></script>

</body>
</html>
